package com.jxw.update;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.jxw.launcher.MyApp;
import com.jxw.launcher.R;
import com.jxw.launcher.util.CheckFileAndDown;
import com.jxw.launcher.util.Logger;
import com.zzj.filedownloader.download.DownLoadListener;
import com.zzj.filedownloader.download.DownLoadManager;
import com.zzj.filedownloader.download.TaskInfo;
import com.zzj.filedownloader.download.dbcontrol.bean.SQLDownLoadInfo;
import java.io.File;
import net.lingala.zip4j.util.InternalZipConstants;

/* loaded from: classes.dex */
public class Fragment4 extends Fragment {
    private DownLoadManager downLoadManager;
    private AlertDialog exitDialog;
    boolean isDownload;
    int len;
    private UpdateActivity mAct;
    private TextView mTextView;
    private View mView;
    private AlertDialog netDialog;
    private ProgressBar pb_progressbar;
    int position;
    View pro;
    Button startBtn;
    Button stopBtn;
    TextView text_count;
    TextView text_pro;
    View tips;

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        this.mView = layoutInflater.inflate(R.layout.fragment_3, viewGroup, false);
        this.startBtn = (Button) this.mView.findViewById(R.id.start_check);
        this.stopBtn = (Button) this.mView.findViewById(R.id.stop_check);
        this.mAct = (UpdateActivity) getActivity();
        this.pb_progressbar = (ProgressBar) this.mView.findViewById(R.id.pb_progressbar);
        this.text_pro = (TextView) this.mView.findViewById(R.id.text_pro);
        this.tips = this.mView.findViewById(R.id.tips);
        this.pro = this.mView.findViewById(R.id.pro);
        this.text_count = (TextView) this.mView.findViewById(R.id.text_count);
        this.downLoadManager = new DownLoadManager(MyApp.getInstance());
        this.downLoadManager.setAllTaskListener(new DownloadManagerListener());
        this.position = 0;
        this.isDownload = false;
        this.startBtn.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.1
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                if (Fragment4.this.haveNet()) {
                    Fragment4 fragment4 = Fragment4.this;
                    fragment4.isDownload = true;
                    fragment4.pro.setVisibility(0);
                    Fragment4.this.stopBtn.setVisibility(0);
                    Fragment4.this.startBtn.setVisibility(8);
                    Fragment4 fragment42 = Fragment4.this;
                    fragment42.downPoi(fragment42.position);
                    TextView textView = Fragment4.this.text_count;
                    textView.setText(Fragment4.this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment4.this.len);
                    StringBuilder sb = new StringBuilder();
                    sb.append(Fragment4.this.len);
                    sb.append("");
                    Log.e("len:", sb.toString());
                }
            }
        });
        this.stopBtn.setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.2
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment4.this.showExitDialog();
            }
        });
        return this.mView;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void downPoi(int i) {
        try {
            if (this.mAct.paths != null) {
                this.len = this.mAct.paths.size();
                if (this.len > i) {
                    if (new File(Environment.getExternalStorageDirectory().getAbsolutePath() + this.mAct.paths.get(i)).exists()) {
                        this.position++;
                        this.text_count.setText(this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + this.len);
                        downPoi(this.position);
                        Log.e("position:", this.position + "+1");
                        int i2 = this.position;
                        int i3 = this.len;
                        Log.e("len:", i3 + "+1");
                        Log.e("po", i2 + "+1");
                        if (i2 == i3) {
                            showDialog_sure();
                        }
                    } else {
                        CheckFileAndDown.downFile(this.mAct.paths, i, this.downLoadManager);
                        int i4 = this.position;
                        int i5 = this.len;
                        Log.e("gwjlen:", i5 + "+2");
                        Log.e("gwjpo", i4 + "+2");
                        if (i4 == i5) {
                            showDialog_sure();
                        }
                    }
                } else {
                    this.downLoadManager.stopAllTask();
                    this.stopBtn.setVisibility(8);
                    this.startBtn.setVisibility(0);
                    this.pro.setVisibility(8);
                    this.tips.setVisibility(8);
                    Toast.makeText(getContext(), "无文件缺失请放心使用", 0).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exit() {
        this.downLoadManager.stopAllTask();
        this.stopBtn.setVisibility(8);
        this.startBtn.setVisibility(0);
        this.pro.setVisibility(8);
        this.tips.setVisibility(8);
    }

    public void downFile(String str, String str2) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str);
        try {
            File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + str + ".tmp");
            if (file2.exists()) {
                Logger logger = Logger.getLogger();
                logger.d("deleteTask=" + file2.getAbsolutePath());
                file2.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.downLoadManager.setSupportBreakpoint(true);
        this.downLoadManager.addTask(str, str2, file.getName(), file.getAbsolutePath());
        this.downLoadManager.startTask(str);
    }

    /* loaded from: classes.dex */
    private class DownloadManagerListener implements DownLoadListener {
        private DownloadManagerListener() {
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onStart(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onStart,name=" + sQLDownLoadInfo.getTaskID());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onProgress(SQLDownLoadInfo sQLDownLoadInfo, boolean z) {
            TaskInfo taskInfo = Fragment4.this.downLoadManager.getTaskInfo(sQLDownLoadInfo.getTaskID());
            if (taskInfo != null) {
                taskInfo.setDownFileSize(sQLDownLoadInfo.getDownloadSize());
                taskInfo.setFileSize(sQLDownLoadInfo.getFileSize());
                taskInfo.setOnDownloading(true);
                taskInfo.getProgress();
                Fragment4.this.pb_progressbar.setProgress(taskInfo.getProgress());
                TextView textView = Fragment4.this.text_pro;
                textView.setText(taskInfo.getProgress() + "%");
                Log.e("task1:", taskInfo.getProgress() + "");
                Log.e("task1:", "sql" + sQLDownLoadInfo.getDownloadSize());
                Log.e("task1:", "sqll" + sQLDownLoadInfo.getFileSize());
            }
            Logger logger = Logger.getLogger();
            logger.d("onProgress,name=" + sQLDownLoadInfo.getTaskID() + ",size=" + sQLDownLoadInfo.getDownloadSize());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onStop(SQLDownLoadInfo sQLDownLoadInfo, boolean z) {
            Logger logger = Logger.getLogger();
            logger.d("onStop,name=" + sQLDownLoadInfo.getTaskID());
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onSuccess(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onSuccess,name=" + sQLDownLoadInfo.getTaskID());
            Fragment4 fragment4 = Fragment4.this;
            fragment4.position = fragment4.position + 1;
            TextView textView = Fragment4.this.text_count;
            textView.setText(Fragment4.this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment4.this.len);
            if (Fragment4.this.position < Fragment4.this.len) {
                Fragment4 fragment42 = Fragment4.this;
                fragment42.downPoi(fragment42.position);
                return;
            }
            Fragment4.this.isDownload = false;
            CheckFileAndDown.isAllDownSus = true;
            new Handler().postDelayed(new Runnable() { // from class: com.jxw.update.Fragment4.DownloadManagerListener.1
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        Fragment4.this.downLoadManager.stopAllTask();
                        Fragment4.this.stopBtn.setVisibility(8);
                        Fragment4.this.startBtn.setVisibility(0);
                        Fragment4.this.pro.setVisibility(8);
                        Fragment4.this.tips.setVisibility(8);
                        Toast.makeText(MyApp.getInstance(), "文件下载完成", 0).show();
                    } catch (Exception unused) {
                    }
                }
            }, AdaptiveTrackSelection.DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS);
        }

        @Override // com.zzj.filedownloader.download.DownLoadListener
        public void onError(SQLDownLoadInfo sQLDownLoadInfo) {
            Logger logger = Logger.getLogger();
            logger.d("onError,name=" + sQLDownLoadInfo.getTaskID());
            Fragment4.this.downLoadManager.deleteTask(sQLDownLoadInfo.getTaskID());
            Fragment4 fragment4 = Fragment4.this;
            fragment4.position = fragment4.position + 1;
            TextView textView = Fragment4.this.text_count;
            textView.setText(Fragment4.this.position + InternalZipConstants.ZIP_FILE_SEPARATOR + Fragment4.this.len);
            if (Fragment4.this.position < Fragment4.this.len) {
                Fragment4 fragment42 = Fragment4.this;
                fragment42.downPoi(fragment42.position);
                return;
            }
            Fragment4 fragment43 = Fragment4.this;
            fragment43.isDownload = false;
            fragment43.downLoadManager.stopAllTask();
            Fragment4.this.stopBtn.setVisibility(8);
            Fragment4.this.startBtn.setVisibility(0);
            Fragment4.this.pro.setVisibility(8);
            Fragment4.this.tips.setVisibility(8);
            CheckFileAndDown.isAllDownSus = true;
            Toast.makeText(Fragment4.this.getContext(), "无文件缺失请放心使用", 0).show();
        }
    }

    private void showDialog_sure() {
        final AlertDialog create = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_update_sure, (ViewGroup) null);
        ((TextView) inflate.findViewById(R.id.cancel_btn)).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.3
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                create.cancel();
                create.dismiss();
            }
        });
        create.setView(inflate);
        create.setCancelable(false);
        create.setCanceledOnTouchOutside(false);
        create.show();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showExitDialog() {
        AlertDialog alertDialog = this.exitDialog;
        if (alertDialog != null) {
            alertDialog.show();
            return;
        }
        this.exitDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog_exitdown, (ViewGroup) null);
        inflate.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.4
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment4.this.exitDialog.cancel();
            }
        });
        inflate.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.5
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment4.this.downLoadManager.stopAllTask();
                Fragment4.this.stopBtn.setVisibility(8);
                Fragment4.this.startBtn.setVisibility(0);
                Fragment4.this.pro.setVisibility(8);
                Fragment4.this.tips.setVisibility(8);
                Fragment4.this.exitDialog.cancel();
            }
        });
        this.exitDialog.setView(inflate);
        this.exitDialog.setCancelable(false);
        this.exitDialog.setCanceledOnTouchOutside(false);
        this.exitDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { // from class: com.jxw.update.Fragment4.6
            @Override // android.content.DialogInterface.OnKeyListener
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return i == 84;
            }
        });
        this.exitDialog.show();
    }

    private void showNetDialog() {
        AlertDialog alertDialog = this.netDialog;
        if (alertDialog != null) {
            alertDialog.show();
            return;
        }
        this.netDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog_nonet, (ViewGroup) null);
        inflate.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.7
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment4.this.netDialog.cancel();
            }
        });
        inflate.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment4.8
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                try {
                    Fragment4.this.startActivity(new Intent("android.settings.WIFI_SETTINGS"));
                    Fragment4.this.netDialog.cancel();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        this.netDialog.setView(inflate);
        this.netDialog.setCancelable(false);
        this.netDialog.setCanceledOnTouchOutside(false);
        this.netDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { // from class: com.jxw.update.Fragment4.9
            @Override // android.content.DialogInterface.OnKeyListener
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return i == 84;
            }
        });
        this.netDialog.show();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean haveNet() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) getContext().getSystemService("connectivity")).getActiveNetworkInfo();
        if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable()) {
            showNetDialog();
            return false;
        }
        return true;
    }
}
